home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-11-30 | 8.6 KB | 416 lines | [TEXT/CWIE] |
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** MPSortPicts a demo of MP Tasking
- **
- ** by Matthew Xavier Mora, Former Apple Developer Technical Support
- **
- ** File: SortPicts.cp
- **
- ** Copyright © 1996-1997 Apple Computer, Inc.
- ** All rights reserved.
- **
- ** You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes.
- */
-
- /*
-
- Macintosh Developer Technical Support MP Sample Code
-
- SortPicts modified by Matthew Xavier Mora
- to show MP sorting and blitting preemptively
-
- mxmora@best.com
- last modified 11/2/98
-
- Added global Boolean for the case where MPLibrary is not around or doesn't load correctly
- Added more blitters to support more bit depths. Blitters based on George Warnes CopyBlits.c
-
- */
-
- #define BUILDING_FOR_SYSTEM7_AND_SYSTEM8 1
-
- #include "MPBlitter.h" // my simple 8 bit blitter
- #include "MPCopyBlits.h" // George's Blitters modified to us MP
-
- #include "MPSortPicts.h"
- //#include "StandardMenus.h"
- #include <string.h>
- #include <strings.h>
- #include <Drag.h>
- #include <LowMem.h>
- #include <Sound.h>
- #include <Menus.h>
- /*
- enum {
- kDragHasLeftSenderWindow = (1 << 0), //drag has left the source window since TrackDrag
- kDragInsideSenderApplication = (1 << 1), // drag is occurring within the sender application
- kDragInsideSenderWindow = (1 << 2) // drag is occurring within the sender window
- };
- */
- long SortPicts::gNumWindowsCreated = 0;
- short SortPicts::gPrefsRef = 0;
- short gSortAlgorithm = 1;
- long gNumSortPictResources = 0;
- MenuHandle gPicturesMenu = 0;
- Boolean gUseCoplandTasks = false;
- short gTaskNumber = 1;
- extern Boolean gHasMPLib;
- Rect gMenuRect;
-
- pascal short MyMBarHook(Rect *menuRect);
- pascal short MyMBarHook(Rect *menuRect)
- {
- (menuRect); // #pragma unused(menuRect)
-
- // should check and see if menurect intersects any of our blitters
- if (gHasMPLib) {
- // MPYield(); // DON'T EVER CALL MPYIELD!
- }
- // RgnHandle tempRgn = NewRgn();
- // RectRgn(tempRgn,menuRect);
- // gMenuRect = *menuRect;
- // CalcVisBehind(LMGetWindowList(),tempRgn);
-
- // YieldToAnyThread();
- // DisposeRgn(tempRgn);
- return 0;
- }
- pascal void MyMenuHook(void);
- pascal void MyMenuHook(void)
- {
-
- if (gHasMPLib) {
- MPYield();
- }
-
- //RgnHandle tempRgn = NewRgn();
- //RectRgn(tempRgn,&gMenuRect);
- //CalcVisBehind(LMGetWindowList(),tempRgn);
-
- //YieldToAnyThread();
- //DisposeRgn(tempRgn);
-
- }
-
- SortPicts::SortPicts()
- {
- MenuHandle sortAlgorithmMenu;
- coplandTask = gUseCoplandTasks;
- updateTime = TickCount();
- lastHorizLeft = 0;
- lastHorizRight = 0;
-
- randomSeed = TickCount() & 0x7FFFFFFF;
-
-
- //LMGetMBarHook_UniversalProcPtr;
- LMSetMBarHook(NewMBarHookProc(MyMBarHook));
- LMSetMenuHook(NewMenuHookProc(MyMenuHook));
- if( gPrefsRef == 0)
- {
- sortAlgorithmMenu = GetMenuHandle( mSortAlgorithm);
- if( sortAlgorithmMenu)
- {
- CheckItem( sortAlgorithmMenu, gSortAlgorithm, true);
- CheckItem( sortAlgorithmMenu, iUseCoplandTasks, gUseCoplandTasks);
- }
-
- gPicturesMenu = GetMenuHandle( mPictureMenu);
- if( gPicturesMenu)
- {
- AppendResMenu( gPicturesMenu, 'PICT');
- InsertMenu( gPicturesMenu, -1);
-
- // Count the number of Pictures in the menu
- gNumSortPictResources = CountMItems( gPicturesMenu);
- }
-
- gPrefsRef = OpenPreferencesResFile();
- if( gPrefsRef > 0)
- {
- OpenDefaultSortWindows();
- }
- }
- else
- {
- SelectRandomPicture();
- CreateWindow( kNormalWindow);
- }
- }
-
- SortPicts::SortPicts( ConstStr255Param windowName)
- {
- randomSeed = TickCount() & 0x7FFFFFFF;
- coplandTask = gUseCoplandTasks;
- updateTime = TickCount();
- lastHorizLeft = 0;
- lastHorizRight = 0;
-
- ::strcpy( (char *) sortPictsName, (char *) windowName);
-
- CreateWindow( kNormalWindow);
- }
-
- SortPicts::~SortPicts()
- {
- /* Terminate Threads */
-
- if ( ! coplandTask ) {
- DisposeThread( threadInfo, nil, true);
- } else {
- OSStatus status = 0;
-
- // • kill the copland task.
- //TerminateTask( myTaskID, kTaskOnly, 0, status );
- MPTerminateTask( myTaskID,status );
- UnuseSortData();
- }
-
- /* Deallocate Memory */
- ReleaseSortData();
- DisposeGWorld( sortGWorld);
- }
-
-
-
-
- WindowPtr SortPicts::MakeNewWindow(WindowPtr /* behindWindow */)
- {
- me = nil;
-
- if( PrepareGWorld( (ConstStr255Param) sortPictsName)) {
-
- if (coplandTask) {
- p2cstr(sortPictsName);
- strcat((char*)sortPictsName,"(MP)");
- c2pstr((char*)sortPictsName);
- }
- me = NewCWindow( nil, &windPictRect, (ConstStr255Param) sortPictsName,
- true, 0, (WindowPtr) -1, true, (long) this);
-
-
-
- CGrafPtr gp = (CGrafPtr)me;
-
- if ((gp && coplandTask) && gp->portPixMap[0]->pixelSize != 8 ) {
-
- // no longer need this alert with Geowars help
- //Alert(1128,nil);
-
- }
- MakeThreaded();
-
- }
-
- return me;
- }
-
-
- void
- SortPicts::Activate(Boolean /* activating */)
- {
- // DrawGrowIcon(fWindow);
- }
-
-
- void
- SortPicts::AdjustCursor(EventRecord * /* anEvent */)
- {
- }
-
-
- void
- SortPicts::Draw(void)
- {
-
-
-
- if ( coplandTask ) {
- //CopyFromSortData();
- CopyBlits ( sortPixmap,
- ((CWindowPtr)me)->portPixMap,
- &sortRect,&sortRect,
- sortBlitterBusy);
-
- /*MPCopyBits( ( BitMap *) *sortPixmap,
- ( BitMap *) (*(((CWindowPtr)me)->portPixMap)),
- &sortRect,&sortRect,srcCopy,(RgnHandle)sortBlitterBusy);
- */
-
- } else {
- sortPixmap = GetGWorldPixMap( sortGWorld);
- LockPixels( sortPixmap );
-
- CopyBits( (const BitMap *) *sortPixmap,
- (const BitMap *) (*(((CWindowPtr)me)->portPixMap)),
- &sortRect,&sortRect,srcCopy,nil);
- UnlockPixels( sortPixmap );
- }
- }
-
-
- // I modified the drag method to do the Live dragging
- void SortPicts:: Drag(Point startPoint)
- {
-
-
- Rect r = {0,0,0,0};
- Point origin = {0,0};
- Point diff;
- EventRecord event;
- GrafPtr tempPort;
- KeyMap theKeyMap;
- Boolean gLiveDrag;
- Boolean commandKeyDown = false;
-
- GetKeys(theKeyMap);
- commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
- gLiveDrag = (theKeyMap[1] >> 2) & 0x01 ;
-
- if (gLiveDrag || coplandTask) {
- GetPort(&tempPort);
- SetPort(fWindow);
- (void)OSEventAvail(everyEvent,&event);
-
- LocalToGlobal(&origin);
- diff.h = ( origin.h - event.where.h);
- diff.v = ( origin.v -event.where.v);
-
- SetPort(tempPort);
- while (StillDown()) {
- EventRecord event;
- (void)WaitNextEvent(everyEvent,&event,1,nil);
-
- if (event.what == updateEvt) {
- WindowRef window = (WindowRef)event.message;
-
-
- if (window) {
- TWindow * wobj;
-
- SetPort(window);
- BeginUpdate(window);
- wobj = GetWindowObject(window);
-
- wobj->Draw();
- EndUpdate(window);
- SetPort(tempPort);
- }
- }
- if (gHasMPLib) {
- MPYield(); // for single CPU Macs
- } else {
- YieldToAnyThread();
- }
- event.where.h += diff.h ;
- event.where.v += diff.v;
-
- if (coplandTask) { // don't move the window if the blitter is blasting bits on the screen
- (void)MPEnterCriticalRegion(sortBlitterBusy, kDurationForever);
- }
- MoveWindow(fWindow,event.where.h,event.where.v,true);
- if (coplandTask) {
- (void)MPExitCriticalRegion(sortBlitterBusy);
- }
- Idle(nil);
-
- }
- }
-
- TWindow::Drag(startPoint); // call the real drag
- if (!coplandTask) {
- Idle(nil);
- }
-
- }
-
-
- void
- SortPicts::Idle(EventRecord * /* anEvent */)
- {
-
-
-
- if ( LMGetTicks() > updateTime )
- {
- UpdateSortPict();
- updateTime = LMGetTicks() + kUpdateInterval;
- // when you're in macsbug LMTicks doesn't get updated
- // so the sorting animation doesn't happen. But the
- // MPTask still sorts and blits the result to the screen
- // even when in macsbug. Cool!
- // I should probably use Uptime here
-
- }
-
-
-
-
- }
-
-
- void
- SortPicts::KeyDown(EventRecord * /* anEvent */ )
- {
- this->Select();
- }
-
-
- void
- SortPicts::AdjustForNewWindowSize(Rect * /* oldSize */, Rect * /* newSize */)
- {
- // DrawGrowIcon(fWindow);
- }
-
-
- Boolean
- SortPicts::Close(void)
- {
-
- DisposeThread( threadInfo, nil, true);
- return TWindow::Close();
- }
-
-
- OSErr
- SortPicts::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
- {
- RgnHandle hiliteRgn = NewRgn();
-
- switch (dragMessage)
- {
- case kDragTrackingEnterWindow:
- SetRectRgn( hiliteRgn,
- fWindow->portRect.left,
- fWindow->portRect.top,
- fWindow->portRect.right,
- fWindow->portRect.bottom);
- ShowDragHilite(theDrag,hiliteRgn,true);
- break;
-
- case kDragTrackingLeaveWindow:
- HideDragHilite(theDrag);
- break;
-
- default:
- break;
- }
-
- DisposeRgn(hiliteRgn);
- return(noErr);
- }
-
-
- OSErr
- SortPicts::HandleDrop(DragReference /* theDrag */)
- {
- return(noErr);
- }
-